/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [SnakeView.java]
 * Creation Date        [21-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.greedysnake_V1;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 贪吃蛇的视图, MVC中的View
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class SnakeView extends JFrame implements Observer {

	private static final long serialVersionUID = -2529725113279330508L;

	// 蛇运行范围格子的宽度和高度
	public static final int nodeWidth = 10;
	public static final int nodeHeight = 10;

	// 游戏画面的宽度和高度
	private final int canvasWidth;
	private final int canvasHeight;

	// 游戏画面左上角的位置
	private int startX = 0;
	private int startY = 0;

	// 显示分数的标签
	JLabel labelScore;
	// 画蛇和格子的画布
	Canvas paintCanvas;

	public SnakeView() {
		this(30, 40, 0, 0);
	}

	public SnakeView(int maxX, int maxY) {
		this(maxX, maxY, 0, 0);
	}

	public SnakeView(int maxX, int maxY, int startX, int startY) {
		canvasWidth = maxX * nodeWidth;
		canvasHeight = maxY * nodeHeight;
		this.startX = startX;
		this.startY = startY;
		init();
	}

	/**
	 * 初始化视图
	 */
	private void init() {

		setName("贪吃蛇");
		this.setLocation(startX, startY);
		Container container = getContentPane();

		// 创建顶部的分数显示
		labelScore = new JLabel("Score:");
		container.add(labelScore, BorderLayout.NORTH);

		// 创建中间的游戏显示区域
		paintCanvas = new Canvas();
		paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
		container.add(paintCanvas, BorderLayout.CENTER);

		// 创建底下的帮助栏
		JPanel panelBottom = new JPanel();
		panelBottom.setLayout(new BorderLayout());

		JPanel panel1 = new JPanel();
		panel1.setLayout(new BorderLayout());
		JLabel labelHelp;
		labelHelp = new JLabel("上下翻页改变速度", JLabel.CENTER);
		panel1.add(labelHelp, BorderLayout.NORTH);
		labelHelp = new JLabel("输入R或S开始", JLabel.CENTER);
		panel1.add(labelHelp, BorderLayout.CENTER);
		panelBottom.add(panel1, BorderLayout.NORTH);

		labelHelp = new JLabel("输入空格或P暂停", JLabel.CENTER);
		panelBottom.add(labelHelp);
		labelHelp = new JLabel("输入Q退出", JLabel.CENTER);
		panelBottom.add(labelHelp);

		container.add(panelBottom, BorderLayout.SOUTH);

		pack();
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	/**
	 * 实现Observer接口定义的update方法
	 */
	@Override
	public void update(Observable o, Object arg) {

		// 获取被监控的模型
		SnakeModel model = (SnakeModel) o;
		// 获得画笔
		Graphics g = paintCanvas.getGraphics();

		// 画背景
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, canvasWidth, canvasHeight);

		// 画蛇
		g.setColor(Color.BLUE);
		LinkedList<Node> nodes = model.getNodeArray();
		Iterator<Node> it = nodes.iterator();
		while (it.hasNext()) {
			Node n = it.next();
			drawNode(g, n);
		}

		// 画食物
		g.setColor(Color.RED);
		Node n = model.getFood();
		drawNode(g, n);
		// 更新分数
		updateScore(model.getScore());
	}

	/**
	 * 画蛇
	 * 
	 * @param g
	 * @param n
	 */
	private void drawNode(Graphics g, Node n) {
		g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
				nodeHeight - 1);
	}

	/**
	 * 更新分数
	 * 
	 * @param score
	 */
	public void updateScore(int score) {
		String s = "Score: " + score;
		labelScore.setText(s);
	}
}